package org.offsync.util

case class ScheduledTask(
    val task: Option[Throwable] => Unit,
    val scheduledTime: ScheduledTime) extends (() => Unit) with (Throwable => Unit) with Ordered[ScheduledTask] {
  def apply() = task(None)
  
  def compare(that: ScheduledTask): Int = scheduledTime.compare(that.scheduledTime)

  def expired: Boolean = scheduledTime.expired

  def apply(throwable: Throwable) = task(Some(throwable))
}

object ScheduledTask {
  def apply(task: () => Unit, scheduledTime: ScheduledTime): ScheduledTask = {
    return ScheduledTask({ _ => task()}, scheduledTime)
  }
  
  val priorityOrdering = new Ordering[ScheduledTask] {
    def compare(x: ScheduledTask, y: ScheduledTask): Int = y.compare(x)
  }
}
